package com.wanshi.mapper;

import com.wanshi.po.Record;
import com.wanshi.po.RecordPo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface RecordMapper {

    @Insert("INSERT INTO record VALUES(NULL,#{timu_id},#{user_id},#{result},#{content},now())")
    void addRecord(Record record);

    @Select("SELECT *,(SELECT content FROM timu WHERE id = timu_id ) timu_content FROM record WHERE user_id = #{user_id} ORDER BY add_time desc")
    List<RecordPo> list(int user_id);

    @Select("SELECT *,(SELECT content FROM timu WHERE id = timu_id ) timu_content FROM record WHERE user_id = #{user_id} and result=#{result} ORDER BY add_time desc")
    List<RecordPo> listByResult(@Param("user_id") Integer user_id, @Param("result") int result);

    @Delete("delete from record WHERE user_id = #{user_id} and id=#{id}")
    void delRecord(@Param("user_id")Integer user_id,@Param("id")int id);


    @Select(" SELECT id,timu_id,user_id,result,add_time,\n" +
            "\t(SELECT content FROM timu WHERE id = timu_id ) timu_content ,\n" +
            "\t(SELECT username FROM user WHERE id = user_id ) username\n" +
            "FROM record\n" +
            "WHERE user_id IN \n" +
            "( SELECT id FROM user WHERE clsname = #{clsname})\n" +
            " ORDER BY add_time DESC")
    List<RecordPo> findRecordByClsName(String clsname);

    //获取所有题目的总分
    @Select("SELECT IFNULL((SELECT SUM(degree) FROM timu),0) score")
    int getTotalDegreeAllTi();

    //获取某用户的总分
    @Select("SELECT IFNULL((SELECT SUM(degree) FROM timu WHERE id IN\n" +
            " (SELECT DISTINCT timu_id\n" +
            "            FROM record\n" +
            "            WHERE user_id = #{user_id}\n" +
            "            AND result = 1)),0) score")
    int getTotalDegreeByUser(int user_id);
}
